﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class DoanhThuTheoLoaiPhongDAO
    {
        public static DataTable LayBaoCaoDoanhThuTheoLoaiPhong(int thangBaoCao, int namBaoCao)
        {
            DataProvider dp = new DataProvider();
            string ngayDauThang = namBaoCao.ToString() + "-" + thangBaoCao.ToString() + "-" + "01";
            string ngay = null;
            switch (thangBaoCao)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    ngay = "31";
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    ngay = "30";
                    break;
                case 2:
                    if (namBaoCao % 4 == 0)
                    {
                        if (namBaoCao % 100 == 0)
                        {
                            if (namBaoCao % 400 == 0)
                            {
                                ngay = "29";
                            }
                            else
                            {
                                ngay = "28";
                            }
                        }
                        else
                        {
                            ngay = "29";
                        }
                    }
                    else
                    {
                        ngay = "28";
                    }
                    break;
            }
            string ngayCuoiThang = namBaoCao.ToString() + "-" + thangBaoCao.ToString() + "-" + ngay;
            string sql = "Select * From BAOCAODOANHTHU Where ThangBaoCao between '" + ngayDauThang + "' and '" + ngayCuoiThang + "'";
            DataTable dt = dp.ExecuteQuery(sql);
            return dt;
        }

        public static Boolean soSanhThangBaoCaoTheoLoaiPhong(int thangBaoCao)
        {
            DataProvider dp = new DataProvider();
            Boolean kq = true;
            string sql = "Select * From BAOCAODOANHTHU Where Month(ThangBaoCao)=" + thangBaoCao;
            DataTable dt = dp.ExecuteQuery(sql);
            if (dt.Rows.Count > 0)
            {
                kq = false;
            }
            return kq;
        }

        public static DataTable GetAll()
        {
            DataProvider dp = new DataProvider();
            string sql = "Select * From BAOCAODOANHTHU";
            DataTable dt = dp.ExecuteQuery(sql);
            return dt;
        }

        public static void ThemDoanhThuTheoLoaiPhong(DoanhThuTheoLoaiPhongDTO dt)
        {
            string sql = string.Format("INSERT INTO BAOCAODOANHTHU (MaBaoCaoDoanhThu, ThangBaoCao, TongDoanhThu) VALUES (" + dt.MaBaoCaoDoanhThu + ", '" + dt.ThangBaoCao + "', " + dt.TongDoanhThu + ")");
            DataProvider.ExecuteNonQuery(sql);
        }
    }
}
